package com.example.demo.beans;

import com.example.demo.proxy.MyService;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class BeanDefinitionExample {
    public static void main(String[] args) {
        // 创建 Spring 上下文（容器）
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 使用 BeanDefinitionBuilder 创建 BeanDefinition
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MyServiceX.class);
        
        // 获取 BeanDefinition 实例
        BeanDefinition beanDefinition = builder.getBeanDefinition();

        // 注册 BeanDefinition 到 Spring 容器
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory();
        registry.registerBeanDefinition("myServiceBean", beanDefinition);  // "myServiceBean" 是 Bean 的名字
        context.refresh();
        // 获取 Bean 并使用
        MyServiceX myService = context.getBean("myServiceBean", MyServiceX.class);

        // 关闭容器
        context.close();
    }
}
